<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript像素图像动画效果</title>
    <link rel="stylesheet" href="./style.css">
</head>
<body>
    <div class="box">
        <div id="pixel"></div>
    </div>
    <script>
        let w = 30;
        let h = 30;

        function pixel() {
            for (var i = 0; i < h; i++) {
                for(var j = 0; j < w; j++) {
                    let span = document.createElement('span');
                    let random = Math.random() * 2;
                    let randomFixed = random.toFixed(2);

                    document.getElementById('pixel').appendChild(span);
                    span.style.left = j * 10 + 'px';
                    span.style.top = i * 10 + 'px';
                    span.style.backgroundPosition = -j*10 + 'px ' + -i*10 + 'px, center';
                    span.style.animationDelay = randomFixed + 's';
                }
            }
        }
        pixel()
    </script>
</body>
</html>